package cn.tansanqinger.controller.stair;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.hymenu.IHyMenuServer;
import cn.tansanqinger.service.user.impl.UserMessageServiceImpl;

@Controller
public class StairIndexController {
	
	@Autowired 
	private UserMessageServiceImpl uImpl;
	@Autowired
	private IHyMenuServer hyMenuServer;
	
	@RequestMapping(value = {"/","index","index.html","index.php","index.do"})
	public ModelAndView hello(HttpServletRequest request, HttpServletResponse response, Model model,ModelAndView mv) {
		model = indexMessage(request, model);
		mv = hyMenuServer.selectHyMenuAll(mv);
		mv.setViewName("stair/index");
		return mv;
	}
	
	public Model indexMessage(HttpServletRequest request, Model model) {
    	HttpSession session = request.getSession();
		Cookie cookie[] = request.getCookies();
		if(cookie!=null) {
			for (int i = 0; i < cookie.length; i++) {
				if("userEmail".equals(cookie[i].getName())){//&&cookie[i].getMaxAge()>0
					String value = cookie[i].getValue();
					if(value!=null) {
						User user = uImpl.selectOneEmail(value);
						session.setAttribute("user",user);
//						List<AccountDispose> list = aImpl.readAll();
//						session.setAttribute("account_dispose",list);
					}
				}
			}
		}
		if(session.getAttribute("user")!=null) {
			model.addAttribute("user", session.getAttribute("user"));
			model.addAttribute("account_dispose", session.getAttribute("account_dispose"));
		}
		return model;
    }
}
